package com.example.controller;

import com.example.pojo.dto.ProductPageDTO;
import com.example.pojo.orm.ProductTypeInfo;
import com.example.pojo.vo.PageVO;
import com.example.service.impl.ProductTypeInfoServiceImpl;
import com.example.util.JsonResult;
import com.example.util.ResultCodeEM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api("季鑫利:商品类别Controller")
@RestController
public class ProductTypeInfoController {

    /**
     * 日志
     */
    private Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 商品类别服务接口
     */
    @Autowired
    private ProductTypeInfoServiceImpl productTypeInfoService;

    @PostMapping("saveProduct")
    @ApiOperation("季鑫利:新增商品类别")
    public JsonResult saveProduct(@ApiParam("商品类别名称") @RequestParam String typeName) {
        JsonResult result;
        try {
            result = productTypeInfoService.saveProduct(typeName);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("新增商品类别异常：--->{}", e.getMessage());
            result = new JsonResult();
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("新增商品类别失败");
            result.setData(e.getMessage());
        }
        return result;
    }

    @PostMapping("removeProduct")
    @ApiOperation("季鑫利:逻辑删除商品类别")
    public JsonResult removeProduct(@ApiParam("商品类别id") @RequestParam Integer id) {
        JsonResult result;
        try {
            result = productTypeInfoService.removeProduct(id);
        } catch (Exception e) {
            log.error("逻辑删除商品类别异常：--->{}", e.getMessage());
            result = new JsonResult();
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("逻辑删除商品类别失败");
            result.setData(e.getMessage());
        }
        return result;
    }

    @PostMapping("updateProduct")
    @ApiOperation("季鑫利:更新商品类别")
    public JsonResult updateProduct(@ApiParam("商品类别名称") @RequestParam String typeName, @ApiParam("商品类别id") @RequestParam Integer id) {
        JsonResult result;
        try {
            result = productTypeInfoService.updateProduct(typeName, id);
        } catch (Exception e) {
            log.error("更新商品类别异常：--->{}", e.getMessage());
            result = new JsonResult();
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("更新商品类别异常");
            result.setData(e.getMessage());
        }
        return result;
    }

    @GetMapping("queryProduct")
    @ApiOperation("季鑫利:查询商品类别")
    public JsonResult<PageVO<ProductTypeInfo>> queryProduct(ProductPageDTO pageDTO) {
        return productTypeInfoService.queryProduct(pageDTO);
    }
}
